/**
 * Copyright (c) 2010 Basil Shikin, BugStat Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.bugstat.generators.reports;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bugstat.common.reports.DateKey;
import org.bugstat.common.reports.ReportKey;
import org.bugstat.common.reports.ReportType;
import org.bugstat.common.reports.ReportValue;
import org.bugstat.common.reports.ReportValueName;
import org.bugstat.common.reports.TupleIntegerValue;
import org.bugstat.common.reports.TupleValueName;
import org.bugstat.common.utils.Comparators;

/**
 * This is a base class for tuple-based reports. Each value is integer tuple.
 *
 * @author Basil Shikin
 */
public class TimedTupleReport
    extends TimedReportBase
{
    protected final int tupleSize;
    
    // Report values (values by date and current max value)
    protected Map<Date, Integer[]> values = new HashMap<Date, Integer[]>();
    protected int maxValue = Integer.MIN_VALUE;
    protected String[]   valueNames         = new String[] {};
    
    public TimedTupleReport(int tupleSize)
    {
        this.tupleSize = tupleSize;
        
        setReportType( ReportType.GRAPH_WITH_AREAS );
    }

    public void incrementValue( final Date date, int offset )
    {
        if ( offset >= tupleSize ) throw new IllegalArgumentException("Inavlid offset: " + offset );
        
        final Integer[] tuple = retrieveTuple( date );
        if ( tuple[ offset ] == null ) tuple[ offset ] = 0;
        
        putValue( date, tuple[ offset ] + 1, offset);
    }
    
    public void putValue( final Date date, Integer value, int offset )
    {
        if ( offset >= tupleSize ) throw new IllegalArgumentException("Inavlid offset: " + offset );
        
        if ( value == null ) value = 0;
        
        Integer[] tuple = retrieveTuple( date );
        tuple[ offset ] = value;
        
        if ( maxValue < value ) maxValue = value;
    }
    
    public void putValue( final Date date, Integer ... newTuple )
    {
        if ( newTuple.length != tupleSize ) throw new IllegalArgumentException("Inavlid tuple size: " + newTuple.length );
        
        values.put( date, newTuple );
        
        for ( int i = 0; i < newTuple.length; i++ )
        {
            if ( newTuple[i] > maxValue ) maxValue = newTuple[i];
        }
    }
    
    @Override
    public int getMaxValue()
    {
        return maxValue;
    }

    public void clear()
    {
        values.clear();
    }
    
    /*
     * BEGIN IMPLEMENTATION OF org.bugstat.common.reports.Report
     */
    
    public ReportValue getValueFor(ReportKey key)
    {
        if ( key instanceof DateKey )
        {
            final Integer[] vals = values.get(((DateKey)key).getDate());
            
            if ( vals != null )
            {
                return new TupleIntegerValue( vals );
            }
            else
            {
                return new TupleIntegerValue( tupleSize );
            }
        }
        else
        {
            throw new IllegalArgumentException("Specified key is not DateKey: " + key );
        }
    }

    public List<? extends ReportKey> getKeys()
    {        
        final List<DateKey> result = new ArrayList<DateKey>();
        
        if ( timeInterval != null )
        {
            // Return all dates in time interval
            Date fromDate = timeInterval.getStartDate();
            Date toDate   = timeInterval.retrieveNextDate( fromDate );
            
            while ( toDate != null )
            {
                result.add( new DateKey( toDate ) );
                
                fromDate = toDate;
                toDate   = timeInterval.retrieveNextDate( toDate );
            }
        }
        else
        {
            for ( Date date : values.keySet() )
            {
                result.add( new DateKey( date ) );
            }
            Collections.sort( result, Comparators.dateKeyComparator );
        }
        
        return result;
    }

    public void setValueNames(String ... reportValueName)
    {
        this.valueNames = reportValueName;
    }
    
    public ReportValueName getValueName()
    {
        return new TupleValueName( valueNames );
    }
    /*
     * END IMPLEMENTATION OF org.bugstat.common.reports.Report
     */
    
    protected Integer[] retrieveTuple( final Date date )
    {
        Integer[] tuple = values.get( date );
        if ( tuple == null )
        {
            tuple = new Integer[ tupleSize ];
            values.put( date, tuple );
        }
        
        return tuple;
    }

}
